import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class jiaojian {
    public static void main(String[] args) throws IOException {
        // 生成数量
        int num=5000;
        String s = "";
        // 随机加减法
        while (num > 0){
            int b = getIntNumber(1,4);
            switch (b) {
                case 1 :{
                    s = s + andAdd2(1,10);
                }
                case 2 :{
                    s = s + andSub2(1,10);
                }
                case 3 :{
                    s = s + addAndSub(1,10);
                }
            }
            num--;
        }


        System.out.println(s);
        //写入文件
        FileWriter writer = new FileWriter("e:/add.txt");
        writer.write(s);
        writer.flush();
        writer.close();
    }


    //随机数
    public static int getIntNumber(int min ,int max){
        Random random = new Random();
        return random.nextInt((max-min)+1)+min;
    }
    // 加法，如果相加大于10则重找
    public static String  add(int min ,int max){
        int first = getIntNumber(min,max);
        int second = getIntNumber(min,max);
        while (first+second > max){
            first=getIntNumber(min,max);
            second = getIntNumber(min,max);
        }
        return String.valueOf(first) + " "+ "+"+" "+String.valueOf(second)+" = "+"\r\n";
    }
    // 减法，如果第一数大于最大数重新，如果第二数大于第一数，重新
    public static String  sub(int min ,int max){
        int first = getIntNumber(min,max);
        int second = getIntNumber(min,max);
        while (second > first){
            second = getIntNumber(min,max);
        }
        return String.valueOf(first) + " "+ "-"+" "+String.valueOf(second)+" = "+"\r\n";
    }
    // 带括号加法
    public static String add2(int min, int max){
        int first = getIntNumber(min,max);
        int second = getIntNumber(min,max);
        // 加法结果如果小于加数，则继续重选随机数
        while (second <= first){
            first = getIntNumber(min,max);
            second = getIntNumber(min,max);
        }
        int or = getIntNumber(1,2);
        if (or == 1) {
            return first + " + " +"(   )"+" =" + second + "\r\n";
        }else {
            return "(   ) + "+first+ " =" + second + "\r\n";
        }
    }
    // 带括号减法
    public static String sub2(int min, int max){
        int first = getIntNumber(min,max);
        int second = getIntNumber(min,max);
        int or = getIntNumber(1,2);
        if (or == 1) {
            // 被减数如果小于结果重选随机
            while (first<=second){
                first = getIntNumber(min,max);
                second = getIntNumber(min,max);
            }
            return first + " - " +"(   )"+" =" + second + "\r\n";
        }else {
            // 减数和结果如果大于max则重新随机
            while (first+second > max){
                first = getIntNumber(min,max);
                second = getIntNumber(min,max);
            }
            return "(   ) - "+first+ " =" + second + "\r\n";
        }
    }
    //三连加
    public static String andAdd(int min, int max){
        int one = getIntNumber(min,max);
        int two = getIntNumber(min,max);
        int three = getIntNumber(min,max);
        while (one + two + three >=max) {
            one = getIntNumber(min,max);
            two = getIntNumber(min,max);
            three = getIntNumber(min,max);
        }
        String result = one + " + " + two + " + " + three + " = " + "\r\n";
        return result;
    }
    //三连加带括号
    public static String andAdd2(int min, int max){
        int one = getIntNumber(min,max);
        int two = getIntNumber(min,max);
        int three = getIntNumber(min,max);
        while (one + two >= three) {
            one = getIntNumber(min,max);
            two = getIntNumber(min,max);
            three = getIntNumber(min,max);
        }
        String result = "";
        int n = getIntNumber(1,3);
        if (n == 1 ){
            result = "(   ) + " + one + " + " + two + " = " + three + "\r\n";
        }else if (n == 2) {
            result = one + " + (   ) + " + two + " = " + three + "\r\n";
        }else if (n == 3){
            result = one + " + " + two + " + (   ) = " + three + "\r\n";
        }else {
            result = one + " + " + two + " + " + three +" = (   )" + "\r\n";
        }
        return result;
    }
    //三连减
    public static String andSub(int min, int max){
        int one = getIntNumber(min,max);
        int two = getIntNumber(min,max);
        int three = getIntNumber(min,max);
        while (one < (two + three)) {
            one = getIntNumber(min,max);
            two = getIntNumber(min,max);
            three = getIntNumber(min,max);
        }
        return one + " - " + two + " - " + three + " = " + "\r\n";
    }
    //三连减带括号
    public static String andSub2(int min, int max){
        int one = getIntNumber(min,max);
        int two = getIntNumber(min,max);
        int three = getIntNumber(min,max);
        String result = "";
        int n = getIntNumber(1,3);
        // 被减数空，则其他减加和结果相加不大于最大数，其他两个见代码
        if (n == 1 ){
            while (one + two + three > max) {
                one = getIntNumber(min,max);
                two = getIntNumber(min,max);
                three = getIntNumber(min,max);
            }
            result = "(   ) - " + one + " - " + two + " = " + three + "\r\n";
        }else if (n == 2) {
            while (one - two - three < 0) {
                one = getIntNumber(min,max);
                two = getIntNumber(min,max);
                three = getIntNumber(min,max);
            }
            result = one + " - (   ) - " + two + " = " + three + "\r\n";
        }else {
            while (one - two - three < 0) {
                one = getIntNumber(min,max);
                two = getIntNumber(min,max);
                three = getIntNumber(min,max);
            }
            result = one + " - " + two + " - (   ) = " + three + "\r\n";
        }
        return result;
    }
    //三连加减混合,感谢某某群友提供的逻辑
    public static String addAndSub(int min , int max) {
        //思路：先随机正确的算式，然后随机替换某一个数字为括号
        int one = getIntNumber(min,max);
        int two = getIntNumber(min,max);
        int three = getIntNumber(min,max);
        int order = getIntNumber(1,2);
        String result = "";
        //先加后减，后先减后加
        if (order == 1) {
            while (one + two > max || one + two < three) {
                one = getIntNumber(min,max);
                two = getIntNumber(min,max);
                three = getIntNumber(min,max);
            }
            int o = getIntNumber(1,4);
            if (o == 1)  {
                int last = one + two - three;
                result = "(   ) + " + two + " - " + three + " = " + last + "\r\n";
            }else if (o == 2) {
                int last = one + two - three;
                result = one + " + (   )" + " - " + three + " = " + last + "\r\n";
            }else if (o == 3){
                int last = one + two - three;
                result = one +" + " + two + " - (   )" + " = " + last + "\r\n";
            }else {
                result = one +" + " + two + " - " + three + " = (   )" + "\r\n";
            }
        }else {
            while (one < two || one - two + three > max || one - two + three < 0) {
                one = getIntNumber(min,max);
                two = getIntNumber(min,max);
                three = getIntNumber(min,max);
            }
            int o = getIntNumber(1,4);
            if (o == 1)  {
                int last = one - two + three;
                result = "(   ) - " + two + " + " + three + " = " + last + "\r\n";
            }else if (o == 2) {
                int last = one - two + three;
                result = one + " - (   )" + " + " + three + " = " + last + "\r\n";
            }else if (o == 3){
                int last = one - two + three;
                result = one +" - " + two + " + (   )" + " = " + last + "\r\n";
            }else {
                result = one +" - " + two + " + " + three + " = (   )" + "\r\n";
            }
        }
        return result;
    }
}
